#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <syslog.h>



static int  checkipdeny (char *sour,char *area)
{
	
	char *buf;
	char *buffernumber;
	printf( "source= %s  area=%s\n",sour,area);    
	struct in_addr addr1,addr2;
	if (!inet_aton(sour,&addr1))
	{
		return 1;
	}
	buffernumber = (char *) malloc(30);
	buf = strstr(area,"/");
	strcpy(buffernumber,buf+1);   
	//printf("buf = %s\n",buffernumber);
	int ofsize,ipaddr,iparea;

	ofsize = atoi(buffernumber);
	//printf("ofsize = %d\n",ofsize);
	if (ofsize == 0 ) 
	{
		return 2;
	}
	strncpy(buffernumber,area,abs(area-buf));
//	printf("after area buffernumber = %s\n",buffernumber);

	if (!inet_aton(buffernumber,&addr2))
	{
		return 3;
	}
	ofsize = 32 - ofsize;
	ipaddr = addr1.s_addr << ofsize ;
	iparea = addr2.s_addr << ofsize ;
//	printf("ipaddr = %d  iparea = %d \n",ipaddr,iparea);
	if (ipaddr == iparea )
	{
		return 0 ;
	}
	else {
		return 4;
	}
}


int main()
{
	printf("%d\n",checkipdeny("192.168.1.2","192.168.1.1/25"));
	printf("%d\n",checkipdeny("192.168.1.2","192.168.1.1/24"));
	printf("%d\n",checkipdeny("192.168.1.2","192.168.1.1/24"));
	printf("%d\n",checkipdeny("192.168.1.2","192.168.1.1/22"));
	printf("%d\n",checkipdeny("192.168.1.2","192.168.1.1/21"));
	printf("%d\n",checkipdeny("192.168.13.2","192.168.1.1/20"));
	printf("%d\n",checkipdeny("192.168.14.2","192.168.1.1/2"));
	printf("%d\n",checkipdeny("192.168.13.2","192.168.1.1/5"));
	printf("%d\n",checkipdeny("192.168.14.2","192.168.1.1/15"));
	printf("%d\n",checkipdeny("192.168.134.2","192.168.1.1/8"));
	printf("%d\n",checkipdeny("192.168.145.2","192.168.1.1/9"));
	printf("%d\n",checkipdeny("192.168.231.2","192.168.1.1/21"));
	printf("%d\n",checkipdeny("192.168.16.2","192.168.1.1/25"));
	printf("%d\n",checkipdeny("192.168.18.2","192.168.1.1/24"));
	printf("%d\n",checkipdeny("192.168.17.2","192.168.1.1/24"));
	printf("%d\n",checkipdeny("192.168.16.2","192.168.1.1/22"));
	printf("%d\n",checkipdeny("192.168.13.2","192.168.1.1/21"));
	printf("%d\n",checkipdeny("192.168.132.2","192.168.1.1/20"));
	printf("%d\n",checkipdeny("192.168.14.2","192.168.1.1/2"));
	printf("%d\n",checkipdeny("192.168.231.2","192.168.1.1/5"));
	printf("%d\n",checkipdeny("192.168.19.2","192.168.1.1/15"));
	printf("%d\n",checkipdeny("192.168.15.2","192.168.1.1/8"));
	printf("%d\n",checkipdeny("192.168.12.2","192.168.1.1/9"));
	printf("%d\n",checkipdeny("192.168.13.12","192.168.11.1/21"));
	return 0;
}
